<?php


namespace App\Services\goods;


use App\Common\statement\CommonStatement;
use App\Models\goods\GoodsCate;

class GoodsService
{
    public static function getCate($pid = 0,$sort = 0,$pre = ''):array
    {
        $array =  GoodsCate::select('id','name','pid')
            ->where('pid',$pid)
            ->orderBy('sort')
            ->get()
            ->toArray();

        foreach ($array as $k => $v){
            $array[$k]['name'] =  $pre.$v['name'];
        }

        $array = array_pluck($array, 'name','id');

        if ($sort){
            $array = array_exchange($array,key($array),$sort);
        }
        return $array;
    }

    public static function getAllCate():array
    {
        $array = self::getCate();

        $rows = [];
        $rows[0] = "主分类";


        foreach ($array as $pid => $value){

            $rows[$pid] = $value;
            $items = self::getCate($pid,0,'└── ');
            foreach ($items as $i =>  $item){
                $rows[$i] = $item;
//                $ls = self::getCate($i,0,'├────  ');
//                foreach ($ls as $l =>  $s){
//                    $rows[$l] = $s;
//                }
            }

        }
        return $rows;
    }


    public static function getTop($pid = 0):int
    {
        return GoodsCate::where('pid',$pid)->orderBy('sort')->value("id")??0;
    }


    public static function getTopId($id = 0):int
    {
        return GoodsCate::where('id',$id)->orderBy('sort')->value("pid")??0;
    }

    public function adminGetCate():array
    {
        $array = self::getAllCate();
//        $array[0] = '主分类';
//        asort($array);
        return $array;
    }

}